// SPDX-License-Identifier: GPL-2.0
/* smc_operate.c - operations of LomboTech SMC module
 *
 * Copyright (C) 2016-2018, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/sched.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/platform_device.h>
#include <mach/debug.h>
#include "smc.h"

//static void __iomem *base;

/* system gpio function */
int smc_gpio_get(struct platform_device *pdev)
{
	struct device_node *np = NULL;
	int ret, err;


	/* 1. get gpio number info from dts
	 * property name out must be consistent with dts
	 */
	ret = of_get_named_gpio(np, "smc_gpio", 0);
	if (ret < 0) {
		PRT_ERR("smc%d gpio property not found\n", pdev->id);
		return -1;
	}

	/* 2. check gpio is valid */
	if (!gpio_is_valid(ret)) {
		PRT_ERR("smc%d gpio %d invalid\n", pdev->id, ret);
		return -1;
	}

	/* 3. request gpio */
	err = devm_gpio_request(&pdev->dev, ret, "smc_gpio");
	if (err < 0) {
		PRT_ERR("smc%d request ret%d fail\n", pdev->id, ret);
		return err;
	}
	return 0;

}

void smc_set_pin_input(unsigned int val)
{
	//gpio_direction_input(gpio);
}

void smc_set_pin_output(unsigned int val)
{
	//gpio_direction_output(gpio, val);
}

int smc_select_gpio_state(struct pinctrl *pctrl, char *name, u32 no)
{
	int ret = 0;
	struct pinctrl_state *pctrl_state = NULL;

	pctrl_state = pinctrl_lookup_state(pctrl, name);
	if (IS_ERR(pctrl_state)) {
		PRT_ERR("smc%d: pinctrl_lookup_state(%s) failed! return %p\n",
			no, name, pctrl_state);
		return -1;
	}

	ret = pinctrl_select_state(pctrl, pctrl_state);
	if (ret < 0) {
		PRT_ERR("smc%d: pinctrl_select_state(%s) failed! return %d\n",
			no, name, ret);
	}

	return ret;
}

void smc_request_gpio(struct pinctrl *pctrl, char *name)
{

}
